package com.example.controller;

import com.example.common.Result;
import com.example.entity.GreenHouse;
import com.example.service.GreenHouseService;
import com.example.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/greenHouse")
public class GreenHouseController {
    @Autowired
    private GreenHouseService greenHouseService;
    @Autowired
    private UserService userService;
    //返回大棚
    @PostMapping("/list/greenhouse")
    public Result listGreenhouse(@RequestBody GreenHouse greenHouse){
        return greenHouseService.listGreenHouse(greenHouse);
    }
    //新增大棚
    @PostMapping("/add/greenhouse")
    public Result addGreenhouse(@RequestParam Integer uid, @RequestBody GreenHouse greenHouse){
        if(userService.isAdmin(uid)){
            return Result.error("当前用户没有权限");
        }
        return greenHouseService.addGreenHouse(greenHouse);
    }

    //删除大棚
    @PostMapping("/delete/greenhouse")
    public Result deleteGreenhouse(@RequestParam Integer uid, @RequestBody GreenHouse greenHouse){
        if(userService.isAdmin(uid)){
            return Result.error("当前用户没有权限");
        }

        return greenHouseService.deleteGreenHouse(greenHouse);
    }

    //更新大棚
    @PostMapping("/update/greenhouse")
    public Result updateGreenhouse(@RequestParam Integer uid, @RequestBody GreenHouse greenHouse){
        if(userService.isAdmin(uid)){
            return Result.error("当前用户没有权限");
        }
        return greenHouseService.updateGreenhouse(greenHouse);
    }

    //返回大棚所有采集点对应类型传感器的平均数据
    @PostMapping("/get/average/all")
    public Result getAverageAll(@RequestBody GreenHouse greenHouse){
        return greenHouseService.getAverageAll(greenHouse);
    }

    //返回大棚各个时间段采集点对应类型传感器的平均数据
    @PostMapping("/get/average/time")
    public Result getAverageTime(@RequestBody GreenHouse greenHouse){
        return greenHouseService.getAverageTime(greenHouse);
    }

    //返回大棚各个采集点对应类型传感器的平均数据
    @PostMapping("/get/average/one")
    public Result getAverageOne(@RequestBody GreenHouse greenHouse){
        return greenHouseService.getAverageOne(greenHouse);
    }

    //返回大棚各个采集点对应类型传感器的平均数据
    @PostMapping("/get/average/max")
    public Result getAverageMax(@RequestBody GreenHouse greenHouse){
        return greenHouseService.getAverageMax(greenHouse);
    }

}
